# -*- coding: utf-8 -*-
import wsgiref.handlers
from google.appengine.ext import webapp
import sys, time, uuid
import os
from google.appengine.ext.webapp import template
from google.appengine.ext import db
from xmw import tianya

from xmw.tianya import error_tianya

class MainPage(webapp.RequestHandler):
  def get(self):
    if  self.request.GET.has_key('thread_id'):
        thread_id = self.request.GET['thread_id']
        
        q = tianya.Thread.all()
        q.filter("thread_id = ", thread_id)        
        thread = q.get()   
            
        if (thread is None):
            #没有找到
            e = error_tianya.THREAD_NOT_EXIST("thread id :%s not found!" % thread_id)
            e.log()
            template_values = {
                               "error":e
                               }
            path = os.path.join(os.path.dirname(__file__), 'template/error.html')
            self.response.out.write(template.render(path, template_values))
        else:
            #找到
            template_values = {
                               "thread":thread
                               }
            path = os.path.join(os.path.dirname(__file__), 'template/query_thread.html')
            self.response.out.write(template.render(path, template_values))
        
    else:
        e = error_tianya.ARGUMENT_FAULT("URI:query_thread needs parameter thread_id.    /query_thread?thread_id=XXXX")
        e.log()
        template_values = {
                           "error":e
                           }     
        path = os.path.join(os.path.dirname(__file__), 'template/error.html')
        #self.response.out.write(path)
        self.response.out.write(template.render(path, template_values))
        
        #render_to_response('wiki/edit.html', {'pagename':pagename})

def main():
    import global_setting
    
    application = webapp.WSGIApplication(
                                       [('/query_thread', MainPage)],
                                       debug=global_setting.is_debug())                                       
    wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
    main()
